<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
#main {
	width: 100%;
	background: #fff;
	position: relative;
	margin: 0px auto;
}

.color1 {
	color: #000;
	font-style: inherit;
}
</style>
<link rel="stylesheet" href="/Hospital/lib/layui-v2.1.6/layui/css/layui.css">
<link rel="stylesheet" href="/Hospital/resources/css/table.css" media="all">
<body>
	<div id="main">
		<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
			<legend>员工管理</legend>
			<div class="div_right">
				<button id="add" class="layui-btn btn_right">
					<i class="layui-icon icon">&#xe61f;</i> 添加
				</button>
				<label class="search_label layui-form-label "> <i class="layui-icon color1">&#xe615;搜索:</i>
				</label> <input type="text" id="search" class="search_input" placeholder="工号/姓名">
			</div>
		</fieldset>

		<div id="middle">
			<table class="layui-table" lay-skin="line row" id="treatmentProjectTable" lay-filter="tableListen" lay-even>
			</table>
		</div>
		<input type="hidden" id="iframeWidth" value=""> <input type="hidden" id="iframeHeight" value="">
	</div>
</body>
<script type="text/javascript" src="/Hospital/lib/jquery.min.js"></script>
<script type="text/javascript" src="/Hospital/lib/layui-v2.1.6/layui/layui.js"></script>
<script type="text/javascript" src="/Hospital/resources/js/util.js"></script>

<!-- //具有编辑客户权限的 -->
<script id="barDemo1" type="text/html" > 
    <a class="layui-btn layui-btn-mini" lay-event="check">详情</a>
    <a class="layui-btn layui-btn-danger layui-btn-mini" lay-event="edit">编辑</a>
</script>
<!-- //不具有编辑客户权限的 -->
<script id="barDemo2" type="text/html" >
    <a class="layui-btn layui-btn-mini" lay-event="check">详情</a>
</script>
<script>
	var loginUser = JSON.parse(localStorage.getItem("loginUser"));
	
	var tableWidth = $(document.body).width() * 0.2;	//数据表格大列宽
	var tableHeight = $(document.body).height() * 0.780; //数据表格高度
	var rightFixed = $(document.body).width() * 0.13;  //数据表格操作宽度
	var numberWidth = $(document.body).width() * 0.1;	//数据表格小列宽度
	
	var employeeData = new Array();//表格数据
	layui.use([ 'table', 'laydate' ], function() {
		var $ = layui.jquery;
		var table = layui.table; //调用layui插件里的表格模块
		var layer = layui.layer; //调用layui插件里的弹出层
		initFrame();
		//resize(); //屏幕自适应
		
		
		
		function initFrame() {
			if(loginUser.permission.createEmployee != 5) {	//如果不是管理员
				//隐藏增加按钮
				hide("#add");
			}
			
			renderTable(); //获取员工信息
			listen(); //监听事件
		}
		
		function listen() {
			var iframeWidth = $(document.body).width()*0.35;
			var iframeHeight = $(document.body).width()*0.9;
			$("#iframeHeight").val(iframeHeight);
			$("#iframeWidth").val(iframeWidth);
			
			
			searchEvent();//搜索框监听事件
			addEvent();//顶部添加项按钮监听
			
			table.on('tool(tableListen)', function(obj) { //编辑、删除按钮监听
				var data = obj.data; //获取当前行的数据
				console.log("employeeManage.data");
				console.log(data);
				var layEvent = obj.event; //获取layEvent对应的值
				var tr = obj.tr; //获取当前tr的DOM对象
				if (layEvent == 'check') { //点击详情时
					var detailedData = {
						'age' : data.age,
						'birthdate' : data.birthdate,
						'registerTime' : data.registerTime,
						'loginTime' : data.loginTime,
						'loginIp' : data.loginIp,
						'status' : data.status,
						'isDomain' : data.isDomain
					};
					localStorage.setItem("detailedData", JSON.stringify(detailedData));
					layer.open({
						title : "详情",
						type : 2,
						area : ["380px", "389px"],
						content : "/Hospital/views/employeeManager/employeeDetailed.html",
						cancel : function(index, layero) {
							localStorage.removeItem("detailedData");
						}
					});
				}
				if (layEvent == 'edit') { //点击编辑时
					//employeeManager_operator : 0(增加);1(编辑);
					localStorage.setItem("employeeManager_operator", 1);
					var editData = {
						'account' : data.account,
						'pwd' : data.pwd,
						'employeeId' : data.employeeId,
						'employeeCode' : data.employeeCode,
						'name' : data.name,
						'positionId' : data.positionId,
						'departmentId' : data.departmentId,
						'sex' : data.sex,
						'sexInt' : data.sexInt,
						'age' : data.age,
						'birthdate' : data.birthdate,
						'isOnJob' : data.isOnJob,
						'isDomain' : data.isDomainInt,
						'isOnJobInt' : data.isOnJobInt,
						'isDomainInt' : data.isDomainInt,
					};
					localStorage.setItem("editTableData", JSON.stringify(editData));//传值localStorage到编辑框
					layer.open({ //弹出编辑框
						title : "编辑",
						type : 2,
						area : [ '500px', '500px' ],
						content : ["/Hospital/views/employeeManager/addEmployee.html","no"],
						cancel : function(index, layero) {
							localStorage.removeItem("editTableData"); //关闭编辑框时消除传值的localStorage
						},
						btn : [ "更改", "取消" ],
						btn1 : function(layero, index) {
							var iframeWin = window[index.find('iframe')[0]['name']];
							
							var isSuccess = iframeWin.submit(); //调用弹出框的submit函数
							console.log("编辑 isSuccess");
							console.log(isSuccess);
							if (isSuccess == 200) {
								var param = {
									"clinicId" : loginUser.clinicId,
								};
								renderTable();
								layer.close(layero);
								layer.msg("更改成功");
							} else if (isSuccess == 411) {
								layer.msg("账号重复");
							} else if (isSuccess == 412) {
								layer.msg("工号重复");
							} else if (isSuccess == 501) {
								layer.msg("数据库插入错误");
							}
							localStorage.removeItem("editTableData"); //关闭编辑框时消除传值的localStorage
						}
					});
				}
			});
			
			function addEvent() {	//顶部添加项按钮监听
				$('#add').click(function() { 
					//employeeManager_operator : 0(增加);1(编辑);
					localStorage.setItem("employeeManager_operator", 0);
					layer.open({ //弹出新增框
						title : "新增",
						type : 2,
						area : [ "500px", "500px"],
						content : "/Hospital/views/employeeManager/addEmployee.html",
						btn : [ "新增", "取消" ],
						btn1 : function(layero, index) { //点击弹出框新增按钮触发事件
							var iframeWin = window[index.find('iframe')[0]['name']]; //获取弹出框对象
							var isSuccess = iframeWin.submit(); //调用弹出框的submit函数
							if (isSuccess == 200) {
								var param = {
										"clinicId" : loginUser.clinicId,
								};
								layer.msg("添加成功");
								table.reload('treatmentProjectTable', {
									type : 'POST',
									url : "/Hospital/userController/employeeAchieveData",
						            where : param,
						            response : {
						               statusName : "code", //数据状态的字段名称
						               statusCode : 200, //成功的状态码
						               countName: 'total', //数据总数的字段名称
						               msgName  : "msg", //状态信息的字段名称
						               dataName : "result" //数据列表的字段名称 
						            }
								});
							} else if (isSuccess == 411) {
								layer.msg("账号重复");
							} else if (isSuccess == 412) {
								layer.msg("工号重复");
							} else if (isSuccess == 501) {
								layer.msg("数据库插入错误");
							}
							table.render();
						},
						btn2 : function(layero, index) { //点击弹出框取消按钮触发事件
						}
					});
				});
			}
			
			function searchEvent() {	//搜索框监听事件
				$('#search').change(function() {
					var search = $('#search').val();
					var clinicId = JSON.parse(localStorage.getItem("loginUser")).clinicId;
					var data = {
						"clinicId" : clinicId,
						"search" : search
					};
					//console.log("search.change.data");
					//console.log(data);
					$.ajax({
						data : data,
						type : 'POST',
						url : "/Hospital/userController/searchEmployeeData",
						success : function(result) {
							if (result.code != 200) {
								console.error(result);
								return;
							}
							table.render({
								elem : '#treatmentProjectTable',
								id:'treatmentProjectTable',
								center : true,
								page : true, //分页
								limit : 20, //每页显示条数
								height : tableHeight,
								cols : createCols(loginUser.permission),
								data : result.result
							});
						}
					}); 
					
				});
			}
		}
		function renderTable() {
			/* console.log("renderTable.tableHeight");
			console.log(tableHeight);
			console.log("renderTable.$(document.body).height()2");
			console.log($(document.body).height()); */
			var param = {
				"clinicId" : loginUser.clinicId,
			};
			table.render({
				id:'treatmentProjectTable',
				elem : '#treatmentProjectTable',
				center : true,
				page : true, //分页
				limit : 10, //每页显示条数
				height : tableHeight,
				cols : createCols(loginUser.permission),
				type : 'POST',
				url : "/Hospital/userController/employeeAchieveData",
	            where : param,
	            request: {
					pageName: 'pageIndex', //页码的参数名称，默认：page
					limitName: 'pageSize' //每页数据量的参数名，默认：limit
	            },  
	            response : {
	               statusName : "code", //数据状态的字段名称
	               statusCode : 200, //成功的状态码
	               countName: "count", //数据总数的字段名称
	               msgName  : "msg", //状态信息的字段名称
	               dataName : "result" //数据列表的字段名称 
	            }
			});
		}
		function resize() {
			//屏幕自适应
			$(window).resize(function() {
				renderTable();
			});
		}
	});
	//生成表格的列
	function createCols(permission) {
		//console.log("clinicTable/createCols");
		//没有修改科室权限
		console.log("createCols.permission");
		console.log(permission);
		if(permission.alterEmployee == 0) {	//非管理员
			//var tableWidth=window.innerWidth*0.141;
			cols = [ [ 
			            {field : 'employeeCode',width : tableWidth,title : '工号',align : 'center'}, 
						{field : 'name',width : tableWidth,title : '姓名',align : 'center'},
						{field : 'positionName',width : numberWidth,title : '职位',align : 'center'}, 
						{field : 'departmentName',width : numberWidth,title : '科室',align : 'center'}, 
						{field : 'sex',width : numberWidth,title : '性别',align : 'center'}, 
						{field : 'isOnJob',width : numberWidth,title : '状态',align : 'center'}, 
						{fixed : 'right',width : rightFixed,align : 'center',toolbar : '#barDemo2'} 
					] ];
			return cols;
		}
		//管理员
		//var tableWidth=window.innerWidth*0.131;
		var cols = [ [ 
			            {field : 'employeeCode',width : tableWidth,title : '工号',align : 'center'}, 
						{field : 'name',width : tableWidth,title : '姓名',align : 'center'},
						{field : 'positionName',width : numberWidth,title : '职位',align : 'center'}, 
						{field : 'departmentName',width : numberWidth,title : '科室',align : 'center'}, 
						{field : 'sex',width : numberWidth,title : '性别',align : 'center'}, 
						{field : 'isOnJob',width : numberWidth,title : '状态',align : 'center'}, 
						{fixed : 'right',width : rightFixed,align : 'center',toolbar : '#barDemo1'} 
					] ];
		return cols;
	}
	
</script>
</html>